package cn.com.spotty.springdemo.controller;


import cn.com.spotty.springdemo.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;



@Controller
public class IndexController {
	static int count = 0;

	@Autowired
	User user;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Model model) {
		++count;
		model.addAttribute("count", count);
		model.addAttribute("user", user);
		return "index";
	}


	@RequestMapping(value = "/balance", method = RequestMethod.GET)
	public String balance(Model model) {
		model.addAttribute("msg", "查询余额为:");
		model.addAttribute("type", "query");
		model.addAttribute("user", user);
		return "balance";
	}

	@RequestMapping(value = "/plus", method = RequestMethod.GET)
	public String plus(HttpServletRequest request, Model model) {
		String num = request.getParameter("num");
		user.setBalance(user.getBalance() + Integer.parseInt(num));
		model.addAttribute("msg", "存了:");
		model.addAttribute("type", "add");
		model.addAttribute("num", num);
		model.addAttribute("user", user);
		return "balance";
	}

	@RequestMapping(value = "/minus", method = RequestMethod.GET)
	public String minus(HttpServletRequest request, Model model) {
		String num = request.getParameter("num");
		user.setBalance(user.getBalance() - Integer.parseInt(num));
		model.addAttribute("msg", " 取了:");
		model.addAttribute("type", "minus");
		model.addAttribute("num", num);
		model.addAttribute("user", user);
		return "balance";
	}
}
